import 'package:adaptive_dialog/adaptive_dialog.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';

class HLAlert {
  /*
  * 弹框只有一个按钮
  * */
  static showSureAlert({
    String? title,
    String? message,
    String okLabel = "确认",
    Function(bool didPop)? sureAction,
  }) async {
    var result = await showOkAlertDialog(
      context: Get.context!,
      title: title,
      message: message,
      okLabel: okLabel,
      style: AdaptiveStyle.iOS,
      barrierDismissible: false,
    );
    if (sureAction != null) {
      sureAction(result == OkCancelResult.ok);
    }
  }

  /*
    * 弹框有两个按钮
    * */
  static showSureCancelAlert({
    String? title,
    String? message,
    String okLabel = "确认",
    String cancelLabel = "取消",
    AdaptiveDialogBuilder? builder,
    Function(bool isSure)? sureAction,
  }) async {
    var result = await showOkCancelAlertDialog(
      context: Get.context!,
      title: title,
      message: message,
      okLabel: okLabel,
      cancelLabel: cancelLabel,
      barrierDismissible: false,
      builder: builder,
      // canPop: true,
    );
    if (sureAction != null) {
      sureAction(result == OkCancelResult.ok);
    }
  }

  /*
    * confirmation按钮
    * */
  static showConfirmation({
    String title = "",
    String? message,
    String? okLabel,
    String cancelLabel = " 取消",
    double? contentMaxHeight,
    int? initialSelectedActionKey,
    List<String> menuList = const [],
    Function(int index, String text)? action,
  }) async {
    List<AlertDialogAction<int>> actions = [];
    for (int i = 0; i < menuList.length; i++) {
      var alertAction = AlertDialogAction<int>(
          key: i,
          label: menuList[i],
          textStyle: TextStyle(
            color: Colors.blue,
            fontSize: 16,
            fontWeight: FontWeight.bold,
          ));
      actions.add(alertAction);
    }
    var result = await showConfirmationDialog<int>(
      context: Get.context!,
      title: title,
      initialSelectedActionKey: initialSelectedActionKey,
      message: message,
      cancelLabel: cancelLabel,
      contentMaxHeight: contentMaxHeight,
      actions: actions,
    );
    if (action != null && result != null) {
      action(result, menuList[result]);
    }
  }

  /*
    * showSheet
    * */
  static showSheet({
    String title = "",
    String? message,
    String? okLabel,
    String cancelLabel = " 取消",
    double? contentMaxHeight,
    int? initialSelectedActionKey,
    List<String> menuList = const [],
    Function(int index, String text)? action,
  }) async {
    List<SheetAction<int>> actions = [];
    for (int i = 0; i < menuList.length; i++) {
      var alertAction = SheetAction<int>(
        key: i,
        label: menuList[i],
        isDestructiveAction: (initialSelectedActionKey != null && initialSelectedActionKey == i),
        textStyle: TextStyle(color: Colors.blue, fontSize: 16, fontWeight: FontWeight.bold),
      );
      actions.add(alertAction);
    }
    var result = await showModalActionSheet<int>(
      context: Get.context!,
      title: title,
      message: message,
      cancelLabel: cancelLabel,
      actions: actions,
    );
    if (action != null && result != null) {
      action(result, menuList[result]);
    }
  }

  /*
  * 谈输入框
  * */
  static showTextFiled({
    String? title,
    String? message,
    String okLabel = "确认",
    String cancelLabel = "取消",
    String? hintText,
    Function(String text)? sureAction,
  }) async {
    List<String>? result = await showTextInputDialog(
      context: Get.context!,
      textFields: [DialogTextField(hintText: hintText)],
      title: title,
      message: message,
      okLabel: okLabel,
      cancelLabel: cancelLabel,
      barrierDismissible: false,
    );
    if (result != null && sureAction != null) {
      sureAction(result.first);
    }
  }
}
